#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
typedef int datatype;
typedef struct listnode
{
	struct listnode* next;
	datatype data;
}SLN;
SLN* listInit()
{
	SLN* ps = (SLN*)malloc(sizeof(SLN));
	if (ps == NULL)
	{
		printf("malloc fail\n");
		return NULL;
	}
	ps->next = NULL;
	return ps;
}
void Listinsert_tail(SLN* ps, datatype x)
{
	SLN* tail = ps;
	SLN* newnode = (SLN*)malloc(sizeof(SLN));
	if (newnode == NULL)
	{
		printf("malloc fail");
		return;
	}
	newnode->next = NULL;
	newnode->data = x;
	while (tail->next != NULL)
	{
		tail = tail->next;
	}
	tail->next = newnode;
}
void Listinsert_head(SLN* ps, datatype x)
{
	SLN* newnode = (SLN*)malloc(sizeof(SLN));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	newnode->next = ps->next;
	ps->next = newnode;
}
void ListPrint(SLN* ps)
{
	SLN* cur = ps->next;
	while (cur != NULL)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
bool is_empty(SLN* ps)
{
	if (ps->next == NULL)
		return true;
	else
		return false;
}
bool listNode_delete(SLN* ps, datatype x)
{
	if (is_empty(ps))
		return false;
	SLN* cur = ps->next;
	SLN* tail = ps;
	while (cur != NULL)
	{
		if (cur->data == x)
		{
			tail->next = cur->next;
			free(cur);
			return true;
		}
		tail = cur;
		cur = cur->next;
	}
}

int main()
{
	SLN* phead = listInit();
	Listinsert_tail(phead, 0);
	Listinsert_tail(phead, 1); 
	Listinsert_tail(phead, 2);
	ListPrint(phead);
	Listinsert_head(phead, -1);
	ListPrint(phead);
	listNode_delete(phead, 0);
	ListPrint(phead);
	listNode_delete(phead, -1);
	ListPrint(phead);
	return 0;
}